package model

import (
	"fmt"
	"ginblog/utils"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

var db *gorm.DB
var err error

func InitDb()  {

	//user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local
	db,err = gorm.Open(mysql.Open(fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", utils.DbUser,utils.DbPassword,utils.DbHost,utils.DbPort,utils.DbName)),

		&gorm.Config{NamingStrategy: schema.NamingStrategy{SingularTable: true,}})
	//禁用默认表名的复数形式
	if err != nil {
		fmt.Println("连接数据库失败，请检查数据库")
	}

	//模型自动转换
	db.AutoMigrate(&User{},&Article{},&Category{})

}